/**
 * 
 */
package de.coe.hendricjabs.friendmap.server;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import de.coe.hendricjabs.friendmap.Contact;
import de.coe.hendricjabs.friendmap.ConversationTypes;
import de.coe.hendricjabs.friendmap.server.exceptions.SystemException;
import de.coe.hendricjabs.friendmap.server.exceptions.SystemException.ErrorType;


/**
 * @author Hendric
 * 
 */
public class ContactRequest extends Request {

    private static final long serialVersionUID = 1L;
    private ArrayList<Contact> contacts;    
    /**
     * @return the contacts
     */
    public ArrayList<Contact> getContacts() {
        return contacts;
    }

    /**
     * @param contacts the contacts to set
     */
    public void setContacts(ArrayList<Contact> contacts) {
        this.contacts = contacts;
    }

    /**
     * @param t
     * @param mobile
     */
    public ContactRequest(String mobile) {
        super(ConversationTypes.CT_CONTACT, mobile);
    }

    /**
     * @param t
     * @param mobile
     */
    public ContactRequest() {
        super(ConversationTypes.CT_CONTACT);
    }
    
    public static ContactRequest fromJSON(String s) throws JsonParseException, JsonMappingException, IOException {
        File temp = File.createTempFile("jsonRequest", ".tmp");

        BufferedWriter bw = new BufferedWriter(new FileWriter(temp));
        bw.write(s);
        bw.close();

        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
        try {
            return mapper.readValue(temp, ContactRequest.class);
            
        } catch (JsonMappingException e) {
            throw new SystemException(ErrorType.INVALID_CONVERSATION_FORMAT);
        }

    }
   
}
